package com.zeny.edu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeny.edu.common.EduException;
import com.zeny.edu.eduservice.entity.EduCourse;
import com.zeny.edu.eduservice.entity.EduCourseDescription;
import com.zeny.edu.eduservice.entity.EduSubject;
import com.zeny.edu.eduservice.entity.dto.CourseAllInfoDto;
import com.zeny.edu.eduservice.entity.dto.CourseAndTeacherAllInfo;
import com.zeny.edu.eduservice.entity.dto.CourseInfoForm;
import com.zeny.edu.eduservice.entity.query.CourseQuery;
import com.zeny.edu.eduservice.mapper.EduCourseMapper;
import com.zeny.edu.eduservice.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zeny
 * @since 2020-03-02
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduSubjectService eduSubjectService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;

    /**
     * @param courseInfoForm
     * @return java.lang.String
     * @Description 保存课程信息
     * @Date 2020/3/3 0003 14:46
     **/
    @Transactional
    @Override
    public synchronized String saveCourceInfo(CourseInfoForm courseInfoForm) {
        if (StringUtils.isNotBlank(courseInfoForm.getId())) {
            //更新
            EduCourse eduCourse = new EduCourse();
            BeanUtils.copyProperties(courseInfoForm, eduCourse);
            QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
            wrapper.eq("id", eduCourse.getId());
            int update = baseMapper.update(eduCourse, wrapper);
            if (update == 0) {
                throw new EduException(20002, "课程信息更新失败");
            }
            EduCourseDescription eduCourseDescription = new EduCourseDescription();
            eduCourseDescription.setId(eduCourse.getId());
            eduCourseDescription.setDescription(courseInfoForm.getDescription());
            UpdateWrapper<EduCourseDescription> wrapperDesc = new UpdateWrapper<>();
            wrapperDesc.eq("id", eduCourse.getId());
            boolean update1 = eduCourseDescriptionService.update(eduCourseDescription, wrapperDesc);
            if (!update1) {
                throw new EduException(20002, "课程详情信息更新失败");
            }
            return eduCourse.getId();
        }
        //保持基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        //保持课程描述信息
        if (insert == 0) {
            throw new EduException(20002, "课程信息保存失败");
        }
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        boolean save = eduCourseDescriptionService.save(eduCourseDescription);
        if (!save) {
            throw new EduException(20002, "课程详情信息保存失败");
        }
        return eduCourse.getId();
    }

    /**
     * @param id
     * @return com.zeny.edu.eduservice.entity.dto.CourseInfoForm
     * @Description 根据ID查找课程信息
     * @Date 2020/3/3 0003 16:46
     **/
    @Override
    public CourseInfoForm selectCourseInfoById(String id) {
        //查找基本信息
        EduCourse eduCourse = baseMapper.selectById(id);
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(eduCourse, courseInfoForm);
        //查找描述信息
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(id);
        courseInfoForm.setDescription(eduCourseDescription.getDescription());

        EduSubject subject = eduSubjectService.getSubjectById(courseInfoForm.getSubjectId());
        courseInfoForm.setSubjectParentId(subject.getParentId());
        return courseInfoForm;
    }

    /**
     * @Description 条件分页查询课程列表
     * @Date 2020/3/4 0004 20:53
     * @param coursePage
     * @param courseQuery
     * @return java.util.List<com.zeny.edu.eduservice.entity.EduCourse>
     **/
    @Override
    public IPage<EduCourse> pageQuery(Page<EduCourse> coursePage, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        if (courseQuery == null) {
            //条件为空
            IPage<EduCourse> eduCourseIPage = baseMapper.selectPage(coursePage, wrapper);
            return eduCourseIPage;
        }
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectId = courseQuery.getSubjectId();
        String subjectParentId = courseQuery.getSubjectParentId();
        if (StringUtils.isNotBlank(title)) {
            wrapper.like("title", title);
        }
        if (StringUtils.isNotBlank(teacherId)) {
            wrapper.eq("teacher_id", teacherId);
        }
        if (StringUtils.isNotBlank(subjectId)) {
            wrapper.eq("subject_id", subjectId);
        }
        if (StringUtils.isNotBlank(subjectParentId)) {
            wrapper.eq("subject_parent_id", subjectParentId);
        }
        return baseMapper.selectPage(coursePage, wrapper);
    }

    /**
     * @Description 根据ID删除课程
     * @Date 2020/3/5 0005 12:47
     * @param id
     * @return boolean
     **/
    @Override
    public boolean deleteById(String id) {
        //根据课程ID删除章节
        eduChapterService.deleteChapterByCourseId(id);
        //根据课程ID删除章节中的小节
        eduVideoService.deleteVideoByCourseId(id);
        //根据课程ID删除课程描述
        eduCourseDescriptionService.deleteDescriptionByCourseId(id);
        //删除课程
        int result = baseMapper.deleteById(id);
        return result > 0;
    }

    @Override
    public CourseAllInfoDto getAllCourseInfoById(String courseId) {
        return baseMapper.getAllCourseInfoById(courseId);
    }

    /**
     * @Description 前台课程分页查询
     * @Date 2020/3/10 0010 15:41
     * @param coursePage
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @Override
    public Map<String, Object> getCourseListPage(Page<EduCourse> coursePage) {
        baseMapper.selectPage(coursePage, null);
//每页的数据
        List<EduCourse> records = coursePage.getRecords();
        //总记录数
        long total = coursePage.getTotal();
        //每页显示的记录数
        long size = coursePage.getSize();
        //总页数
        long pages = coursePage.getPages();
        //当前页
        long current = coursePage.getCurrent();
        //是否有下一页
        boolean hasNext = coursePage.hasNext();
        //是否有上一页
        boolean hasPrevious = coursePage.hasPrevious();
        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("size",size);
        map.put("pages",pages);
        map.put("current",current);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        map.put("items",records);
        return map;
    }

    /**
     * @Description 前台课程根据ID查询课程详细信息
     * @Date 2020/3/10 0010 16:36
     * @param id
     * @return com.zeny.edu.eduservice.entity.dto.CourseAndTeacherAllInfo
     **/
    @Override
    public CourseAndTeacherAllInfo getCourseAndTeacherAllInfo(String id) {
        return baseMapper.getCourseAndTeacherAllInfo(id);
    }


}
